package controller;
import java.io.UnsupportedEncodingException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import dao.UserDAO;

import pojo.User;



import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

@Controller
public class TaiKhoanCuaToiController {
	@RequestMapping (value = "KiemTraVaGuiMail.do", method = RequestMethod.GET)
	public @ResponseBody String showKiemTraVaGuiMail(
			@RequestParam(value = "email", required = false) String email,
			@RequestParam(value = "id", required = false) String id,
			HttpServletRequest request)
	{

		HttpSession session2 = request.getSession(false);
		User us = (User) session2.getAttribute("user");
		if(us==null) return "page/FrontEnd/DangNhap";
		UserDAO ud = new UserDAO();
		boolean idn=ud.kiemTraDaTonTaiTenDangNhap(id);
		if(idn==false) return "noid";
		boolean isExist = ud.kiemTraDaTonTaiEmail(email);
		if(!isExist)
		{
			return "notexist";
		}
		else
		{
			String matKhau = ud.layMatKhauQuaEmail(email);
			
			final String username = "pcdinh91@gmail.com";
			final String password = "donatest";
			
			Properties props = new Properties();
			props.put("mail.smtp.auth", "true");
			props.put("mail.smtp.starttls.enable", "true");
			props.put("mail.smtp.host", "smtp.gmail.com");
			props.put("mail.smtp.port", "587");
	 
			Session session = Session.getInstance(props,
			  new javax.mail.Authenticator() {
				protected PasswordAuthentication getPasswordAuthentication() {
					return new PasswordAuthentication(username, password);
				}
			  });
			
			try {
				 
				Message message = new MimeMessage(session);
				message.setFrom(new InternetAddress("congdinhphan@gmail.com"));
				message.setRecipients(Message.RecipientType.TO,
					InternetAddress.parse(email));
				message.setSubject("[Baby Fashion Shop] Receive your forget password");
				message.setText("This is for you: " + matKhau + ". Goodluck to you.");
	 
				Transport.send(message);
				} 
			catch (MessagingException e) {
				return "sendfail";
			}
			return "sendsuccess";
		}
	}
	@RequestMapping (value = "LayLaiMatKhau.do", method = RequestMethod.GET)
	public String showLayLaiMatKhau(HttpServletRequest request
			)
	{
		HttpSession session2 = request.getSession(false);
		User us = (User) session2.getAttribute("user");
	//	if(us==null) return "page/FrontEnd/DangNhap";
		return "page/FrontEnd/LayLaiMatKhau";
	}
	@RequestMapping (value = "DoiMatKhau.do", method = RequestMethod.GET)
	public String showDoiMatKhau()
	{
		return "page/FrontEnd/DoiMatKhau";
	}
	

	
	@RequestMapping (value = "ThongTinCaNhan.do", method = RequestMethod.GET)
	public String showThongTinCaNhan( HttpServletRequest request
			)
	{
		HttpSession session2 = request.getSession(false);
		User us = (User) session2.getAttribute("user");
		if(us==null) return "page/FrontEnd/DangNhap";
		return "page/FrontEnd/ThongTinCaNhan";
	}
	@RequestMapping (value = "ThongTinChungTaiKhoan.do", method = RequestMethod.GET)
	public String show(HttpServletRequest request)
	{
		HttpSession session2 = request.getSession(false);
		User us = (User) session2.getAttribute("user");
		if(us==null) return "page/FrontEnd/DangNhap";
		return "page/FrontEnd/ThongTinChungTaiKhoan";
	}
	
	@RequestMapping (value = "DoiMatKhau.do", method = RequestMethod.POST)
	public @ResponseBody String doimatkhau(
			@RequestParam(value = "MatKhauCu", required = false) String mkcu,
			@RequestParam(value = "MatKhauMoi", required = false) String mkmoi,
			@RequestParam(value = "XacNhan", required = false) String xn,
			HttpServletRequest request
			) throws UnsupportedEncodingException
	{
		request.setCharacterEncoding("UTF-8");
		HttpSession session = request.getSession(false);
		User us = (User) session.getAttribute("user");
		UserDAO ud= new UserDAO();
		if(us==null) return "fail";
		else
		{
			boolean flag=ud.capNhatMatKhau(us, mkcu, mkmoi, xn);
			if(flag==true)
				return "success";
			else
				return "fail";
		}

	}
	@RequestMapping (value = "ThongTinCaNhan.do", method = RequestMethod.POST)
	public @ResponseBody String show(
			@RequestParam(value = "HoVaTen", required = false) String hoTen,
			@RequestParam(value = "Ngay", required = false) String ngay,
			@RequestParam(value = "Thang", required = false) String thang,
			@RequestParam(value = "Nam", required = false) String nam,
			@RequestParam(value = "DiaChi", required = false) String diaChi,
			@RequestParam(value = "Email", required = false) String email,
			@RequestParam(value = "DienThoai", required = false) String dienThoai,
			HttpServletRequest request
			) throws UnsupportedEncodingException
	{
		System.out.println("dasasdasdasd");
		request.setCharacterEncoding("UTF-8");
		HttpSession session = request.getSession(false);
		User kh = (User) session.getAttribute("user");
		UserDAO ud = new UserDAO();
		if(kh != null)
		{
			if (hoTen != "")
				kh.setName(hoTen);
			if (!(ngay.equals("00")) && !(thang.equals("00")) && (nam != ""))
			{
				String NgayThangNam = thang + "/" + ngay + "/" + nam;
				SimpleDateFormat dateFormat = new SimpleDateFormat("mm/dd/yyyy");
				Date _ngayThangNam = null;
				try {
					_ngayThangNam = dateFormat.parse(NgayThangNam);
				} catch (ParseException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				kh.setDateOfBirth(_ngayThangNam);
			}
			if (diaChi != "")
				kh.setAddress(diaChi);
			if (email != "")
				kh.setEmail(email);
			if (dienThoai != "")
				kh.setPhone(dienThoai);
		}
		Boolean successUpdate = ud.capNhatThongTinUser(kh);
		if (successUpdate)
			return "success";
		else
			return "fail";
	}
	public static String getYear(Date d) {
		DateFormat dateFormat = new SimpleDateFormat("yyyy");
		return (dateFormat.format(d));
	}
}
